package com.lych.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.lych.model.mongo.Questions;
import com.lych.model.mongo.SoulUser;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;
import java.util.UUID;

@DubboService
public class SoulUserApiImpl implements SoulUserApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 根据用户id查询问卷用户关系表
     * @param userId
     * @return
     */
    @Override
    public List<SoulUser> findByid(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        return mongoTemplate.find(query,SoulUser.class);
    }

    /**
     * 将分数添加到表中，用于之后查询与你相似，还要将后一级的试卷打开
     * @param score
     * @param id    具体试题题目id
     * @return
     */
    @Override
    public String addScore(Integer score, String id, Long userId) {
        //创建一个报表id
        String soulReportId = UUID.randomUUID().toString();
        //得到问题表中的数据，问题表中有soulId字段
        Query query = Query.query(Criteria.where("id").is(id));
        Questions questions = mongoTemplate.findOne(query, Questions.class);
        //得到soulId
        String soulId = questions.getSoulId();
        //根据userId和soulId查询
        Query query2 = Query.query(Criteria.where("soulId").is(soulId).and("userId").is(userId));
        Update update = Update.update("score", score);
        Update update1 = Update.update("soulReportId", soulReportId);
        mongoTemplate.updateFirst(query2, update, SoulUser.class);
        mongoTemplate.updateFirst(query2,update1,SoulUser.class);

        //开启下一套试题
        //构建soudId
        int i = (Integer.parseInt(soulId)) + 1;
        Query query1 = Query.query(Criteria.where("soulId").is(i+"").and("userId").is(userId));
        Update update2 = Update.update("isLock", 0);
        mongoTemplate.updateFirst(query1,update2,SoulUser.class);

        return soulReportId;
    }

    /**
     * 根据报告id查询对应的分数
     * @param soulReportId
     * @return
     */
    @Override
    public SoulUser findBySoulReportId(String soulReportId) {
        Query query = Query.query(Criteria.where("soulReportId").is(soulReportId));
        SoulUser soulUser = mongoTemplate.findOne(query, SoulUser.class);
        return soulUser;
    }

    /**
     * 查询目前有几套试题
     * @param l         专用测试账号id
     * @param userId
     * @return
     */
    @Override
    public Integer findSize(long l, Long userId) {
        Query query = Query.query(Criteria.where("userId").is(l));
        //根据当前用户id查询该用户是否有试题列表
        Query query1 = Query.query(Criteria.where("userId").is(userId));
        List<SoulUser> soulUsers = mongoTemplate.find(query1, SoulUser.class);
        if (!CollUtil.isEmpty(soulUsers)) {
            return 0;
        }
        List<SoulUser> list = mongoTemplate.find(query, SoulUser.class);
        return list.size();
    }

    /**
     * 问卷用户关系
     * @param soulUser
     */
    @Override
    public void save(SoulUser soulUser) {
        mongoTemplate.save(soulUser);
    }
}
